<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>发布作业 - 析码无界</title>
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <link rel="stylesheet" href="http://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
    <script src="http://cdn.bootcss.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background-color: #f5f5f5;
            padding-top: 20px;
        }
        .panel {
            border: none;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            border-radius: 8px;
        }
        .panel-heading {
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 8px 8px 0 0;
        }
        .btn-primary {
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            border: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title"><i class="fa fa-paper-plane"></i> 发布作业</h3>
            </div>
            <div class="panel-body">
                <div th:if="${error}" class="alert alert-danger" th:text="${error}"></div>
                
                <form th:action="@{/assignment/publish}" method="post">
                    <div class="form-group">
                        <label>作业标题 <span style="color: red;">*</span></label>
                        <input type="text" class="form-control" name="title" required>
                    </div>
                    <div class="form-group">
                        <label>作业内容 <span style="color: red;">*</span></label>
                        <textarea class="form-control" name="content" rows="5" required></textarea>
                    </div>
                    <div class="form-group">
                        <label>选择班级 <span style="color: red;">*</span></label>
                        <small class="help-block" style="display: block; margin-bottom: 5px;">
                            <i class="fa fa-info-circle"></i> 可以按住 Ctrl（Windows）或 Command（Mac）键选择多个班级
                        </small>
                        <select class="form-control" name="classIds" multiple required size="5" style="min-height: 120px;">
                            <option th:each="clazz : ${classes}" 
                                    th:value="${clazz.id}" 
                                    th:text="${clazz.className}"></option>
                        </select>
                        <small class="help-block" style="margin-top: 5px;">
                            已选择 <span id="selectedCount">0</span> 个班级
                        </small>
                    </div>
                    <div class="form-group">
                        <label>截止时间 <span style="color: red;">*</span></label>
                        <input type="datetime-local" class="form-control" name="deadlineStr" required>
                    </div>
                    <button type="submit" class="btn btn-primary">
                        <i class="fa fa-paper-plane"></i> 发布作业
                    </button>
                    <a th:href="@{/assignment/list}" class="btn btn-default">
                        <i class="fa fa-arrow-left"></i> 返回列表
                    </a>
                </form>
            </div>
        </div>
    </div>
    
    <script>
        // 更新已选择班级数量
        function updateSelectedCount() {
            const select = document.querySelector('select[name="classIds"]');
            const count = select.selectedOptions.length;
            document.getElementById('selectedCount').textContent = count;
            
            // 验证至少选择一个班级
            if (count === 0) {
                select.setCustomValidity('请至少选择一个班级');
            } else {
                select.setCustomValidity('');
            }
        }
        
        // 监听选择变化
        document.querySelector('select[name="classIds"]').addEventListener('change', updateSelectedCount);
        
        // 表单提交前验证
        document.querySelector('form').addEventListener('submit', function(e) {
            const select = document.querySelector('select[name="classIds"]');
            if (select.selectedOptions.length === 0) {
                e.preventDefault();
                alert('请至少选择一个班级！');
                return false;
            }
        });
        
        // 初始化
        updateSelectedCount();
    </script>
</body>
</html>

